'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.
/**
 * @fileoverview 获取随便奖励
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');

/**
 * 获取6个随机奖励
 * @param  {[type]} options [description]
 * @return {[type]}         [description]
 */
exports.sample = function (all_conf, num) {
    var total_probability = 0;
    var total_conf_list = [];
    _.each(all_conf, function (conf) {
        total_probability += conf.probability;
        var item = _.clone(conf);
        item.probability = total_probability;
        total_conf_list.push(item);
    });

    var conf_list = [];
    var genFun = function (random) {
        return function (conf) {
            if (random <= conf.probability) {
                conf_list.push(conf);
                return true;
            }
        };
    };

    if (!num) {
        var gen_random = _.random(0, total_probability);
        _.some(total_conf_list, new genFun(gen_random));
        return conf_list[0];
    } else {
        for (var i = 0; i < num; i++) {
            var random = _.random(0, total_probability);
            _.some(total_conf_list, new genFun(random));
        }
        return conf_list;
    }
};
